//
//  PTTextView.m
//  PT
//
//  Created by yate1996 on 15/6/25.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "PTTextView.h"

@interface PTTextView () 
@property (nonatomic, weak) UILabel *placehoderLabel;

@end
@implementation PTTextView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self) {
        self.backgroundColor = [UIColor clearColor];
        
        UILabel *placehoderLabel = [[UILabel alloc]init];
        placehoderLabel.numberOfLines = 0;
        placehoderLabel.backgroundColor = [UIColor clearColor];
        
        [self addSubview:placehoderLabel];
        self.placehoderLabel = placehoderLabel;
        self.placehoderColor = [UIColor lightGrayColor];
        
        self.font = [UIFont systemFontOfSize:14];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
        
    }
    return self;
}

-(void)delloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)textDidChange
{
    self.placehoderLabel.hidden = (self.text.length != 0);
}

-(void)setText:(NSString *)text
{
    [super setText:text];
    
    [self textDidChange];
}

-(void)setPlacehoder:(NSString *)placehoder
{
    _placehoder = [placehoder copy];
    
    self.placehoderLabel.text = placehoder;
    
    [self setNeedsLayout];
}


-(void)setPlacehoderColor:(UIColor *)placehoderColor
{
    _placehoderColor = placehoderColor;
    self.placehoderLabel.textColor = placehoderColor;
}

-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placehoderLabel.font = font;
    
    [self setNeedsLayout];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.placehoderLabel.y = 10;
    self.placehoderLabel.x = 10;
    self.placehoderLabel.width =self.width - 2 * self.placehoderLabel.x;
    CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT);
    CGSize placehoderSize = [self.placehoder sizeWithFont:self.placehoderLabel.font constrainedToSize:maxSize];
    self.placehoderLabel.height = placehoderSize.height;
}



@end
